// app.js
import Notify from '@vant/weapp/notify/notify';
App({
  
  onLaunch() {
    this.openSocket();
  },
  globalData: {
    serviceUrl:"http://localhost:9090/",
    userInfo: null,
    storage:null,
  },
  openSocket() {
      
    let that = this;
    wx.getStorage({
      key:"tokenInfo",
      success:(res)=>{
        //打开时的动作
        wx.onSocketOpen(() => {
          console.log('WebSocket 已连接')
        })
        //断开时的动作
        wx.onSocketClose(() => {
          console.log('WebSocket 已断开')
        })
        //报错时的动作
        wx.onSocketError(error => {
          console.error('socket error:', error)
        })
        // 监听服务器推送的消息
        wx.onSocketMessage(message => {
          if(message.data!=='心跳检测'){
            Notify({
              type:"success",
              message:message.data
            })
          }
        })
        // 打开信道
        wx.connectSocket({
          url: 'ws'+this.globalData.serviceUrl.replace("http","")+'websocket/'+res.data.userType+res.data.tokenInfo.id,
        })
      }
    })
   }
})
